package com.other;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bgt on 2017/12/13.
 *      A
 *    B   C
 *   D E    F
 *
 *
 *        1
 *     2    3
 *    4 5     6
 */
public class ErChaTree
{
    public static void main(String[] args) {
        TreeNode root=new TreeNode(1);
        TreeNode rootleft=new TreeNode(2);
        TreeNode rootright=new TreeNode(3);
        root.left=rootleft;
        root.right=rootright;
        rootleft.left=new TreeNode(4);
        rootleft.right=new TreeNode(5);
        rootright.right=new TreeNode(6);
        PrintFromTopToBottom pptt=new PrintFromTopToBottom();
        pptt.printNode(root);
    }
}

class TreeNode {
    int val=0;//深度
    TreeNode left=null;//左节点
    TreeNode right=null;//右节点

    public TreeNode(int val) {
        this.val = val;
    }
}

interface Solution{
    public ArrayList<ArrayList<Integer>> printNode(TreeNode proot);
}

class PrintFromTopToBottom implements Solution {

    @Override
    public ArrayList<ArrayList<Integer>> printNode(TreeNode proot) {
        ArrayList<ArrayList<Integer>> result=new ArrayList<>();
        List<TreeNode> cur=new ArrayList<TreeNode>();
        cur.add(proot);
        while (cur.size()>0){
            List<TreeNode> tem=new ArrayList<TreeNode>();
            ArrayList<Integer> curval=new ArrayList<>();
            cur.forEach(node->{
                if (node.left!=null)tem.add(node.left);
                if (node.right!=null)tem.add(node.right);
                curval.add(node.val);
            });
            cur=tem;
            result.add(curval);
        }
        System.out.println(result);
        return result;
    }

}
